import os


def get_folder_size(folder_path):
    """计算文件夹的总大小（包含所有子目录）"""
    total_size = 0
    for dirpath, dirnames, filenames in os.walk(folder_path):
        for filename in filenames:
            filepath = os.path.join(dirpath, filename)
            total_size += os.path.getsize(filepath)
    return total_size


def format_size(size_bytes):
    """将字节大小转换为易读格式（KB/MB/GB）"""
    for unit in ['B', 'KB', 'MB', 'GB']:
        if size_bytes < 1024.0:
            return f"{size_bytes:.2f} {unit}"
        size_bytes /= 1024.0
    return f"{size_bytes:.2f} TB"


# 主程序
if __name__ == "__main__":
    # current_dir = os.getcwd()  # 获取当前工作目录
    current_dir = "e:/"  # 获取当前工作目录

    # 获取所有直接子文件夹 (当前层级)
    subfolders = [
        f for f in os.listdir(current_dir)
        if os.path.isdir(os.path.join(current_dir, f))
    ]

    print(f"当前目录: {current_dir}")
    print("-" * 50)
    print("文件夹名称\t大小")
    print("-" * 50)

    for folder in subfolders:
        folder_path = os.path.join(current_dir, folder)
        size_bytes = get_folder_size(folder_path)
        readable_size = format_size(size_bytes)
        print(f"{folder}\t{readable_size}")
